package com.simon.system.controller;

import java.util.List;

import com.simon.exception.OperationFailedException;
import com.simon.http.model.DoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.simon.system.model.Permisson;
import com.simon.system.service.PermissonService;
import com.simon.token.TokenValidate;
import com.simon.utils.SystemCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName PermissonController
 * @Description 权限控制层
 * @Author Jiajie.Mao
 * @Date 2018/6/5 9:38
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/permission")
@Api(value = "permission", tags = {"权限接口"})
public class PermissonController {

    @Autowired
    private PermissonService permissonService;

    @ApiOperation(value = "根据用户获取权限", notes = "根用户id获取权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get/user", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getPermissionByUserId(@RequestParam int userId) {
        List<Permisson> permissons = permissonService.findByUserId(userId);
        if (permissons != null) {
            return new DoResponse<>(permissons);
        } else {
            throw new OperationFailedException();
        }
    }


    @ApiOperation(value = "根据角色获取权限", notes = "根角色id获取权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌",  paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get/role", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getPermissionByRoleId(@RequestParam int roleId) {
        List<Permisson> permissons = permissonService.findByRoleId(roleId);
        if (permissons != null) {
            return new DoResponse<>(permissons);
        } else {
            throw new OperationFailedException();
        }
    }
}
